<template>
  <div class="airway-clean-flow">
    <h4>风道清理流程</h4>
    <div class="main-container">
      <!-- 左侧流程步骤区域 -->
      <div class="flow-container">
        <!-- 步骤一 -->
        <div class="flow-step step-one" @click="showStepOneDialog = true">
          <div class="step-number">1</div>
          <div class="step-content">
            <h5>检查风冷流量情况</h5>
            <p>查看机组控制屏的发电机面板</p>
          </div>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#10b981"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#10b981" />
          </svg>
        </div>

        <!-- 步骤二 -->
        <div
          class="flow-step step-two"
          :class="{
            completed: airwayCleanStore.getStepTwoStatus,
            unlocked: airwayCleanStore.getStepOneStatus,
          }"
        >
          <div class="step-number">2</div>
          <div class="step-content">
            <h5>清理风道</h5>
            <p>执行风道清理操作</p>
          </div>
          <button
            class="clean-airway-button"
            :disabled="!airwayCleanStore.getStepOneStatus"
            @click="showCleanConfirmDialog = true"
          >
            清理风道
          </button>
        </div>

        <!-- 流程箭头 -->
        <div class="flow-arrow">
          <svg width="40" height="20" viewBox="0 0 40 20">
            <line
              x1="0"
              y1="10"
              x2="30"
              y2="10"
              stroke="#10b981"
              stroke-width="2"
            />
            <polygon points="30,5 40,10 30,15" fill="#10b981" />
          </svg>
        </div>

        <!-- 步骤三 -->
        <div
          class="flow-step step-three"
          :class="{
            completed: airwayCleanStore.getStepThreeStatus,
            unlocked: airwayCleanStore.getStepTwoStatus,
          }"
        >
          <div class="step-number">3</div>
          <div class="step-content">
            <h5>检查趋势曲线</h5>
            <p>查看4组发电机定子绕组温度变量</p>
          </div>
          <button
            class="view-trend-button"
            :disabled="!airwayCleanStore.getStepTwoStatus"
            @click="openTrendChart"
          >
            查看趋势曲线
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤一的提示对话框 -->
    <div
      v-if="showStepOneDialog"
      class="dialog-overlay"
      @click="closeStepOneDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>检查机组控制屏的发电机面板</h5>
        <p>请在发电机面板上查看风冷流量情况，确认当前的风冷流量状态。</p>
        <div class="airflow-preview">
          <h6>当前四路风量预览</h6>
          <div class="airflow-grid">
            <div class="airflow-item">
              <span class="airflow-label">第一路 (Dode_Vol5):</span>
              <span class="airflow-value">{{ currentAirflows["2_38_0_0"] || "--" }}</span>
            </div>
            <div class="airflow-item">
              <span class="airflow-label">第二路 (Dode_Vol6):</span>
              <span class="airflow-value">{{ currentAirflows["2_39_0_0"] || "--" }}</span>
            </div>
            <div class="airflow-item">
              <span class="airflow-label">第三路 (Dode_Vol8):</span>
              <span class="airflow-value">{{ currentAirflows["2_40_0_0"] || "--" }}</span>
            </div>
            <div class="airflow-item">
              <span class="airflow-label">第四路 (Dode_Vol10):</span>
              <span class="airflow-value">{{ currentAirflows["2_36_0_0"] || "--" }}</span>
            </div>
          </div>
        </div>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeStepOneDialog">
            取消
          </button>
          <button class="confirm-button" @click="openGeneratorPanel">
            打开发电机面板
          </button>
        </div>
      </div>
    </div>

    <!-- 步骤二的风道清理确认对话框 -->
    <div
      v-if="showCleanConfirmDialog"
      class="dialog-overlay"
      @click="closeCleanConfirmDialog"
    >
      <div class="dialog-content" @click.stop>
        <h5>风道清理确认</h5>
        <p>确定要执行风道清理操作吗？</p>
        <p class="clean-note">清理后，将把四路风量恢复到正常值(2.5)。</p>
        <div class="dialog-buttons">
          <button class="cancel-button" @click="closeCleanConfirmDialog">
            取消
          </button>
          <button
            class="confirm-button clean-button"
            :disabled="airwayCleanStore.getIsCleaning"
            @click="confirmCleanAirway"
          >
            {{ airwayCleanStore.getIsCleaning ? "清理中..." : "确认清理" }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";
import { setAlgorithmParam } from "@/api/util";
import { useUserStore } from "@/store/user";
import { useAirwayCleanStore } from "@/store/airwayCleanStore";

// 定义组件props
const props = defineProps({
  initialAirflows: {
    type: Object,
    default: () => ({
      "2_38_0_0": 2.5,
      "2_39_0_0": 2.5,
      "2_40_0_0": 2.5,
      "2_36_0_0": 2.5,
    }),
  },
});

// 获取用户store实例
const userStore = useUserStore();
// 获取风道清理store实例
const airwayCleanStore = useAirwayCleanStore();

// 定义组件事件
const emit = defineEmits([
  "open-generator-panel",
  "airway-cleaned",
  "open-trend-chart",
]);

// 对话框状态管理
const showStepOneDialog = ref(false);
const showCleanConfirmDialog = ref(false);

// 当前风量参数
const currentAirflows = reactive({
  ...props.initialAirflows,
});

// 需要设置的参数列表
const airflowParams = [
  "2_38_0_0", // 第一路的给定风冷流量(Dode_Vol5)
  "2_39_0_0", // 第二路的给定风冷流量(Dode_Vol6)
  "2_40_0_0", // 第三路的给定风冷流量(Dode_Vol8)
  "2_36_0_0", // 第四路的给定风冷流量(Dode_Vol10)
];

// 关闭步骤一对话框
const closeStepOneDialog = () => {
  showStepOneDialog.value = false;
};

// 关闭清理确认对话框
const closeCleanConfirmDialog = () => {
  showCleanConfirmDialog.value = false;
};

// 打开发电机面板
const openGeneratorPanel = () => {
  // 使用store的action设置步骤一为已完成
  airwayCleanStore.completeStepOne();
  showStepOneDialog.value = false;
  // 触发事件通知父组件打开发电机面板
  emit("open-generator-panel");
};

// 确认清理风道
const confirmCleanAirway = async () => {
  try {
    // 使用store设置清理状态
    airwayCleanStore.setCleaningStatus(true);
    // 从user store获取copyNum
    const copyNum = userStore.copyNum;
    
    // 逐个设置参数值为2.5
    const promises = airflowParams.map((param) =>
      setAlgorithmParam(param, 2.5, copyNum)
    );

    // 等待所有设置完成
    await Promise.all(promises);

    // 更新当前状态
    airflowParams.forEach((param) => {
      currentAirflows[param] = 2.5;
    });

    // 显示成功提示
    ElMessage.success("风道清理操作已成功完成");

    // 关闭对话框
    showCleanConfirmDialog.value = false;

    // 使用store标记步骤二为已完成
    airwayCleanStore.completeStepTwo();

    // 触发风道已清理事件
    emit("airway-cleaned", currentAirflows);
  } catch (error) {
    console.error("清理风道失败:", error);
    ElMessage.error("风道清理操作失败，请重试");
  } finally {
    // 无论成功失败，都设置清理状态为false
    airwayCleanStore.setCleaningStatus(false);
  }
};

// 打开趋势图表
const openTrendChart = () => {
  // 使用store标记步骤三为已完成
  airwayCleanStore.completeStepThree();
  // 触发事件通知父组件打开趋势图表
  emit("open-trend-chart");
};
</script>

<style scoped>
.airway-clean-flow {
  background: #2c3e50;
  border-radius: 8px;
  padding: 24px;
  color: #ecf0f1;
  min-width: 300px;
}

.airway-clean-flow h4 {
  color: #10b981;
  margin-top: 0;
  margin-bottom: 20px;
  padding-bottom: 10px;
  border-bottom: 1px solid #34495e;
  font-size: 18px;
  font-weight: 600;
}

.main-container {
  display: flex;
  gap: 20px;
  min-height: 400px;
}

/* 左侧流程步骤区域 */
.flow-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* 流程步骤样式 */
.flow-step {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background: #34495e;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  border-left: 4px solid #7f8c8d;
}

.flow-step:hover {
  background: #3c566a;
  transform: translateX(5px);
}

.flow-step.unlocked {
  border-left-color: #10b981;
  cursor: pointer;
}

.flow-step.completed {
  border-left-color: #10b981;
  background: #2d3e50;
}

.flow-step.step-two.completed {
  border-left-color: #10b981;
}

.step-number {
  width: 32px;
  height: 32px;
  background: #7f8c8d;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 16px;
}

.flow-step.unlocked .step-number {
  background: #10b981;
}

.flow-step.completed .step-number {
  background: #10b981;
}

.step-content {
  flex: 1;
}

.step-content h5 {
  margin: 0 0 8px 0;
  color: #ecf0f1;
  font-size: 16px;
  font-weight: 600;
}

.step-content p {
  margin: 0;
  color: #95a5a6;
  font-size: 14px;
}

/* 流程箭头 */
.flow-arrow {
  display: flex;
  justify-content: center;
  margin: -8px 0;
}

/* 按钮样式 */
.clean-airway-button, .view-trend-button {
  background: #10b981;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
  font-size: 14px;
}

.clean-airway-button:hover:not(:disabled), .view-trend-button:hover:not(:disabled) {
  background: #059669;
}

.clean-airway-button:disabled, .view-trend-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

/* 对话框样式 */
.dialog-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.dialog-content {
  background: #2c3e50;
  border-radius: 8px;
  padding: 24px;
  min-width: 400px;
  max-width: 600px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
}

.dialog-content h5 {
  margin: 0 0 16px 0;
  color: #10b981;
  font-size: 18px;
  font-weight: 600;
}

.dialog-content p {
  margin: 0 0 12px 0;
  color: #ecf0f1;
  font-size: 15px;
}

.clean-note {
  font-style: italic;
  color: #95a5a6 !important;
  font-size: 14px !important;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.cancel-button {
  background: #7f8c8d;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.cancel-button:hover {
  background: #95a5a6;
}

.confirm-button {
  background: #3498db;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.confirm-button:hover:not(:disabled) {
  background: #2980b9;
}

.confirm-button:disabled {
  background: #7f8c8d;
  cursor: not-allowed;
}

.clean-button {
  background: #10b981;
}

.clean-button:hover:not(:disabled) {
  background: #059669;
}

/* 风量预览样式 */
.airflow-preview {
  background: #34495e;
  border-radius: 6px;
  padding: 16px;
  margin: 16px 0;
}

.airflow-preview h6 {
  margin: 0 0 12px 0;
  color: #95a5a6;
  font-size: 14px;
  font-weight: 500;
}

.airflow-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.airflow-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: #2c3e50;
  border-radius: 4px;
  font-size: 13px;
}

.airflow-label {
  color: #cbd5e1;
  font-size: 12px;
}

.airflow-value {
  color: #10b981;
  font-weight: 600;
  font-family: "Courier New", monospace;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .main-container {
    flex-direction: column;
  }

  .dialog-content {
    min-width: 280px;
    margin: 20px;
  }

  .flow-step {
    flex-direction: column;
    align-items: flex-start;
  }

  .dialog-buttons {
    flex-direction: column;
  }

  .cancel-button, .confirm-button {
    width: 100%;
  }

  .airflow-grid {
    grid-template-columns: 1fr;
  }
}
</style>